home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Macintosh Sample Code / SC.016.OffSample / UFailure.p < prev   
Encoding:
Text File  |  1988-10-31  |  12.5 KB  |  346 lines  |  [TEXT/MPS ]

  1. {------------------------------------------------------------------------------
  2. #
  3. #    Apple Macintosh Developer Technical Support
  4. #
  5. #    Exception handling for MPW Pascal, MacApp and MPW C
  6. #
  7. #    UFailure (aka Signals) - “Exceptional code, with a few exceptions.”
  8. #
  9. #    UFailure.p    -    Pascal source
  10. #
  11. #    Copyright © 1985-1988 Apple Computer, Inc.
  12. #    All rights reserved.
  13. #
  14. #    Versions:    1.0                    11/88
  15. #
  16. #    Components:    UFailure.p            November 1, 1988
  17. #                UFailure.h            November 1, 1988
  18. #                UFailure.inc1.p        November 1, 1988
  19. #                UFailure.a            November 1, 1988
  20. #                TestCignal.c        November 1, 1988
  21. #                TestCignal.make        November 1, 1988
  22. #                TestSignal.p        November 1, 1988
  23. #                TestSignal.make        November 1, 1988
  24. #
  25. #    UFailure (or Signals) is a set of exception handling routines suitable for
  26. #    use with MacApp, MPW C, and MPW Pascal. It is a jazzed-up version of the MacApp
  27. #    UFailure unit. There is a set of C interfaces to it as well.
  28. #
  29. ------------------------------------------------------------------------------}
  30.  
  31.  
  32. {$P}
  33.  
  34. { We assume that MacApp is being used if qTrace is defined. IF not, you may use
  35.     “-mbg off” in the makefile if you don’t want TMONic symbology (i.e. embedded names). }
  36. {$IFC UNDEFINED qTrace}
  37.     {$SETC UsingMacApp := 0}
  38.     {$IFC UNDEFINED qDebug}
  39.         {$SETC qDebug := 0} {default to debugging code off without MacApp}
  40.     {$ENDC qDebug}
  41. {$ELSEC}
  42.     {$SETC UsingMacApp := 1}
  43. {$ENDC qTrace}
  44.  
  45. { Conditional compile flags used by this unit when used with MacApp:
  46.  
  47.     qDebug            True includes debugging code, false ignores debugging code.
  48.  
  49.     qTrace            True surrounds trap patches with $D+ and $D++ (because
  50.                     the default is $D++), false skips these directives.
  51.  
  52.     The settings of $D, $R and $N are imported from UMAUtil.
  53.  }
  54.  
  55. UNIT UFailure;
  56.  
  57. {
  58. Theory:
  59.     See the “theory of operation” comments below, and the commentary in Technical
  60.     Note #88.
  61.     
  62.     *** New ***
  63.     The warnings in technote 88 about not using CatchSignal in an expression, etc. no
  64.     longer apply. The exact state of the routine is now restored (all non-scratch
  65.     registers are preserved). Also, a fixed-size nonrelocatable block is now used, so
  66.     there is a limit on the depth of nested CatchSignals. This may be adjusted by
  67.     changing the constant SigBlockSize in UFailure.a and rebuilding the unit. For most
  68.     applications, though, the default depth of eight will be more than sufficient.
  69.     
  70.     Note: If you use CatchFailures to catch exceptions then you will still be required
  71.     to call Success or FreeSignal explicitly.
  72.     
  73.     *** You can use either the technote mechanism or the MacApp one in your program. It
  74.     is OK to call Signal if CatchFailures was used, and OK to call Failure if
  75.     CatchSignal was used, i.e. the two schemes may be freely intermixed. ***
  76.     
  77.     What this version adds to the MacApp mechanism is two things:
  78.         1.
  79.             Exception records (FailInfo) are taken from a special heap block when
  80.             you use CatchSignal, so you don’t have to pass a FailInfo record as a
  81.             parameter.
  82.         2.
  83.             You can take advantage of stack frames and use of Signals as described
  84.             in Technical Note #88, without being forced to make explicit calls to
  85.             FreeSignal. This unit is fully upwards compatible with the 1986 version
  86.             of the technote.
  87.             
  88.     This includes all of the MacApp calls from the original UFailure, of course.
  89.  
  90.                     T H E O R Y   O F   O P E R A T I O N
  91.  
  92.     This unit implements the MacApp and Signal failure mechanisms.
  93.  
  94.     The failure mechanism is built around exception handlers.  An exception
  95.     handler is a piece of code, generally local to some other routine, that is
  96.     called when a failure occurs and takes action to handle the failure.
  97.     An exception handler is of the form
  98.  
  99.         PROCEDURE ExceptionHandler (error: OSErr; message: LONGINT);
  100.                                     or
  101.         the handler may consist of execution returning to the point of a
  102.         CatchSignal
  103.  
  104.     where error is the error that caused the failure, and message identifies
  105.     the error message that may be displayed.  Consider a routine that opens
  106.     a file, reads its contents, and closes the file.  If a failure occured
  107.     while reading the file, an exception handler would be needed to close the
  108.     file, as the rest of the routine would not be executed.  (See TestCignal
  109.     and TestSignal for examples of how to use these calls.)
  110.  
  111.     References to exception handlers are kept in the FailInfo record. The
  112.     exception handlers form a linked-list via the nextInfo field of FailInfo.
  113.     The linked list is a stack since new exception handlers are added to the
  114.     front of the list.
  115.  
  116.     New exception handlers are added to the stack with the CatchSignal
  117.     function or CatchFailures procedure. They are removed from the stack
  118.     automatically (CatchSignal) or via the Success procedure (if CatchFailures
  119.     was used). In general you call CatchFailures/CatchSignal to post an exception
  120.     handler when an error the application should handle might occur. You
  121.     can then manually pop the last handler off the stack with FreeSignal or
  122.     Success, if necessary. You may want to pop the handler (even if you used
  123.     CatchSignal) after the possibility of a specific type of error no longer
  124.     exists. Subsequent exceptions would then be passed to a previous (more
  125.     general) handler.
  126.     
  127.     Any failure detected within the limits of the CatchFailures/CatchSignal call
  128.     results in the execution of the exception handler.  (Failure does
  129.     not have to occur in the same routine as your call to CatchFailures.
  130.     The failure may occur in any routine called after the catch but before the
  131.     implicit/explicit pop of the handler.)
  132.  
  133.     When MacApp (or your code) determines that a failure has occured, it
  134.     calls Failure or Signal.  As a convenience, several procedures are provided
  135.     to check for standard kinds of failures and call Failure if needed.
  136.     These procedures are:
  137.  
  138.         FailNIL            Calls Failure if its parameter is NIL.
  139.         FailOSErr        Calls Failure if its parameter is not noErr.
  140.         FailMemError    Calls Failure if MemError returns other than noErr.
  141.         FailResError    Calls Failure if ResError returns other than noErr.
  142.  
  143.     When the exception is raised, execution of the routine that signalled is
  144.     terminated and the exception handler at the top of the stack is popped.
  145.     For each routine that was called after the handler was posted
  146.     to the stack, execution is terminated as though from an EXIT statement,
  147.     and the exception handler is called.  It generally cleans up for the
  148.     routine in which it is nested.  Upon completion the next exception handler
  149.     is popped from the stack, repeating the process.
  150.  
  151.     The error causing the failure, and a message code is passed to the handler.
  152.     
  153.     MacApp Specifics
  154.     For MacApp, the last exception handler on the stack is the one in
  155.     TApplication.PollEvent.  It calls TApplication.ShowError, which calls
  156.     ErrorAlert, which decodes the message and displays an alert.  Your exception
  157.     handlers may set the message code to one more specific to your application
  158.     by calling FailNewMessage at the end of your exception handler.
  159.     FailNewMessage changes the message only if the current one is non-zero.
  160.     This has the effect of allowing those exception handlers closest to the
  161.     source of the error to set the message.
  162.  
  163.     One last note about exception handlers:  It is possible for an exception
  164.     handler to terminate exception processing by using a non-local GOTO to
  165.     jump back into the routine in which the exception handler is nested.  This
  166.     is how MacApp keeps the application running when a failure occurs.  The
  167.     last exception handler on the stack, in TApplication.PollEvent, uses a
  168.     GOTO to continue event processing.
  169. }
  170.  
  171. INTERFACE
  172.  
  173. {$IFC UsingMacApp}
  174. USES
  175.     {$LOAD MacIntf.LOAD}
  176.         MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
  177.     {$LOAD}
  178.         UMAUtil
  179.     {$IFC qDebug}
  180.         , UWritelnWindow
  181.         , UTrace
  182.     {$ENDC}
  183.         ;
  184. {$ELSEC}
  185. USES
  186.     MemTypes, QuickDraw, OSIntf, ToolIntf;
  187. {$ENDC UsingMacApp}
  188.  
  189.  
  190. {-----------------------------------+
  191. |    Global types                    |
  192. +-----------------------------------}
  193. TYPE
  194.         { used for the exception handling mechanism }
  195.     PFailInfo =        ^FailInfo;
  196.     FailInfo =        RECORD
  197.                     regs:            ARRAY[1..11] OF LONGINT;
  198.                     error:            INTEGER;
  199.                     message:        LONGINT;
  200.                     failA6:         LONGINT;
  201.                     failPC:         LONGINT;
  202.                     nextInfo:        PFailInfo;
  203.                     {this is needed for MacApp debugging}
  204.                     whoPC:            LONGINT;
  205.                     {these are added for USignalFailure unit use}
  206.                     whatSignals:    INTEGER;
  207.                     sigFRet:        LONGINT;    {used to keep the old stack frame return address}
  208.                     END;
  209.  
  210.  
  211. {-----------------------------------+
  212. |    Global variables                |
  213. +-----------------------------------}
  214. {$IFC UsingMacApp}
  215. {$IFC qDebug}
  216. VAR
  217.     {$PUSH}
  218.     {$Z+}    {make ’em accessable to assembly code}
  219.     gAskAboutAlloc:     BOOLEAN;    { if TRUE ask about each allocation to force
  220.                                         failure }
  221.     gAskFailure:        BOOLEAN;    { if TRUE ask about calls to
  222.                                         FailOSErr, FailResError, FailMemError,
  223.                                         and give user a chance to force error }
  224.     {$POP}
  225. {$ENDC qDebug}
  226. {$ENDC UsingMacApp}
  227.  
  228.  
  229. {-----------------------------------+
  230. |    Global routines                    |
  231. +-----------------------------------}
  232. {
  233. Call one of the two following initialization routines before your other
  234. initializations (InitGraf, etc.)--in other words as early as you can in the
  235. application.
  236. }
  237.  
  238. PROCEDURE InitUFailure;
  239.     { Allocates the heap block for CatchSignals and initializes the global
  240.         variables used by the unit. No CatchSignals from the main level
  241.         of Pascal are allowed if you use InitUFailure. [C programs must use this
  242.         instead of InitSignals.] }
  243.  
  244.  
  245. PROCEDURE InitSignals;
  246.     { Calls InitUFailure. It also sets up A6 at the main level of Pascal,
  247.         so it must be called from the outermost level of Pascal. }
  248.  
  249.  
  250. FUNCTION CatchSignal: INTEGER;
  251.     { Until the procedure which encloses this call returns, this will catch
  252.         subsequent Signal calls, returning the code passed to Signal.  When
  253.         CatchSignal is encountered initially, it returns a code of zero.  These
  254.         calls may "nest"; i.e. you may have multiple CatchSignals in one procedure.
  255.         If you signal with Failure and pass in a non-zero message you may want to use
  256.         CatchFailures instead so you have a way of getting at the message. }
  257.  
  258.  
  259. PROCEDURE FreeSignal;
  260.     { This undoes the effect of the last CatchSignal/CatchFailures.  A Signal will
  261.         then invoke the CatchSignal -prior- to the last one. }
  262.  
  263.  
  264. PROCEDURE Signal(code: INTEGER);
  265.     { Returns control to the point of the last CatchSignal/CatchFailures.  The program will
  266.         then behave as though that CatchSignal had returned with this code parameter.
  267.         If CatchFailures is catching, the message parameter will be 0. }
  268.  
  269.  
  270. PROCEDURE SignalMessage(code: INTEGER; message: LONGINT);
  271.     { Returns control to the point of the last CatchSignal/CatchFailures.
  272.         If CatchFailures is catching, the message parameter will be returned. }
  273.         
  274.         
  275. {-----------------------------------+
  276. |    MacApp routines                    |
  277. +-----------------------------------}
  278.  
  279.  
  280.  
  281. FUNCTION  BuildMessage(lowWord, highWord: INTEGER): LONGINT;
  282.             INLINE $2E9F;    { MOVE.L        (A7)+,(A7)     }
  283.     { Takes the 2 integers and combines them into a LONGINT.  Note that the
  284.         low-order word is the first parameter. }
  285.  
  286. PROCEDURE CatchFailures(VAR fi: FailInfo;
  287.                         PROCEDURE Handler(e: INTEGER; m: LONGINT));
  288.     { Call this to set up an exception handler.    This pushes your handler onto
  289.         a stack of exception handlers. }
  290.  
  291. PROCEDURE Failure(error: INTEGER; message: LONGINT);
  292.     { Call this to signal a failure.  Control will branch to the most recent
  293.         exception handler, which will be popped off the handler stack. }
  294.  
  295. PROCEDURE FailMemError;
  296.     { IF MemError <> noErr THEN Failure(MemError, 0);  If you are using
  297.         assembler, then you should just test the return code from the Memory
  298.         Manager in DO by calling FailOSErr.    (See the discussion of MemError in
  299.         Inside Macintosh.) }
  300.  
  301. PROCEDURE FailResError;
  302.     { IF ResError <> noErr THEN Failure(ResError, 0); (See Inside Macintosh.) }
  303.  
  304. PROCEDURE FailNewMessage(error: INTEGER; oldMessage, newMessage: LONGINT);
  305.     { This does:
  306.         IF oldMessage = 0 THEN
  307.             Failure(error, newMessage)
  308.         ELSE
  309.             Failure(error, oldMessage);
  310.      }
  311.  
  312. PROCEDURE FailNIL(p: UNIV Ptr);
  313.     { Call this with a pointer/handle; this signals Failure(memFullErr, 0) iff
  314.         the pointer is NIL. }
  315.  
  316. PROCEDURE FailOSErr(error: INTEGER);
  317.     { Call this with an OSError; signals Failure(error, 0) iff error <> noErr. }
  318.  
  319. {$IFC UsingMacApp}
  320. PROCEDURE SetInitHandler(handler: ProcPtr);
  321.     { Sets an initialization error handler; applications should not call
  322.         this, normally. }
  323. {$ENDC UsingMacApp}
  324.  
  325. PROCEDURE Success(VAR fi: FailInfo);
  326.     { Call this when you want to de-install your exception handler (pop 1
  327.         element off the handler stack). }
  328.         
  329. {$IFC UsingMacApp}
  330. {$IFC qDebug}
  331. PROCEDURE ProgramBreak(grievance: Str255);
  332.     { Causes a program break to the debug window, displaying grievance. }
  333.  
  334. PROCEDURE ProgramReport(grievance: Str255; break: BOOLEAN);
  335.     { Displays grievance in the debug window, and breaks if break is true. }
  336. {$ENDC qDebug}
  337. {$ENDC UsingMacApp}
  338.  
  339.  
  340.  
  341. IMPLEMENTATION
  342.  
  343. {$I UFailure.inc1.p}
  344.  
  345. END. {UFailure}
  346.